﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Collection.Entities;
using Collection.Core;
using WanPiJie.Products;
using System.Reflection;
using System.Configuration;

namespace Collection.Platform
{
    /// <summary>
    /// 采集器
    /// </summary>
    public class Collector
    {
        #region 类型初始化相关
        /// <summary>
        /// 采集器实例
        /// </summary>
        public static readonly Collector Instance;

        /// <summary>
        /// 类目Id列表
        /// </summary>
        private IList<long> catalogIds;

        //类目Id列表
        public IList<long> CatalogIds
        {
            get { return this.catalogIds; }
        }

        /// <summary>
        /// 静态构造函数
        /// </summary>
        static Collector()
        {
            Instance = new Collector();
        }

        //私有构造（防止公开构造）
        private Collector()
        {
            this.catalogIds = new List<long>();
            var catalogIdsStr = ConfigurationManager.AppSettings["catalogIds"];
            if (!string.IsNullOrEmpty(catalogIdsStr))
            {
                var ids = catalogIdsStr.Split(';');
                foreach (var id in ids)
                {
                    this.catalogIds.Add(Int64.Parse(id));
                }
            }
        }

        #endregion

        /// <summary>
        /// 依据类目Id列表采集相应类目的属性数据信息
        /// <param name="catalogIds">类目Id列表</param>
        /// </summary>
        public void GatherProperties()
        {
            IList<Property> properties;
            foreach (Int64 id in this.catalogIds)
            {
                CatalogCollector.Instance.GatherProperty(id);
                properties = CollectionServiceFactory.PropertyService.GetProperties(id);
                foreach (var property in properties)
                {
                    CatalogCollector.Instance.GatherPropertyValues(id, property.Id);
                }
            }
        }

        /// <summary>
        /// 依据类目Id采集商品数据信息
        /// </summary>
        public void GatherCommodities()
        {
            foreach (Int64 id in this.catalogIds)
            {
                MerchandiseCollector.Instance.GatherCommodities(id);
            }
        }
    }
}
